package com.kh.statistics.video.helper;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.kh.statistics.video.entity.StatisticsMeta;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.Map;

/**
 * @ClassName HttpHelper
 * @Description TODO
 * @Author konghong1
 * @Date 2024/5/9 21:45
 * @Version 1.0
 **/
public class StatisticsHttpHelper {

    public static String request(StatisticsMeta statisticsMeta) {
        String requestType = statisticsMeta.getRequestType();
        if (StringUtils.isBlank(requestType) || "get".equalsIgnoreCase(requestType)) {
            HttpRequest httpRequest = HttpUtil.createGet(statisticsMeta.getUrl());
            return httpRequest.addHeaders(statisticsMeta.getHeaders())
                    .execute().body();
        }else {
            Map<String, Object> params = statisticsMeta.getParams();

            HttpRequest httpRequest = HttpUtil.createPost(statisticsMeta.getUrl())
                    .addHeaders(statisticsMeta.getHeaders());
            if(!CollectionUtils.isEmpty(params)){
                httpRequest
                        .body(JSON.toJSONString(params));
            }
            if(StringUtils.isNotBlank(statisticsMeta.getJsonParam())){
                httpRequest
                        .body(statisticsMeta.getJsonParam());
            }
           return httpRequest.timeout(10000)
                    .execute().body();
        }

    }

}
